#pragma once
#include <string>
#include <utility>
#include <boost/thread.hpp>
#include <stdio.h>
#include <signal.h>

using namespace std;
#define NC 0
#define PLAY 1
#define STOP 2
#define PAUSE 3

enum ioctrl{all,in,out};

class mod_common
{
protected:
    FILE * fd=NULL;
    char *fp;
    bool isplay=false;
    boost::thread *thread_p;
public:
    virtual bool open()=0;
    virtual bool close()=0;
    virtual bool play()=0;
    virtual bool play(int times)=0;
    virtual int stop()=0;
    virtual bool read(char *fp, int len, int val)=0;
    virtual bool write(char *fp, int len, int val)=0;
    virtual bool contrl(mod_input *in)=0;
    virtual bool contrl(mod_output *out)=0;
    virtual bool pause()=0;

    bool isPlay(){
        return isplay;
    }

    bool setExcel(char *str){
        if(fopen(str,"r")==NULL){
            return false;
        }
        return true;
    }
};

class mod_input{
protected :
    char *sql_string=0;
    char *data=0;
    char *path;
    char value_c=0;
    int value=0;
    double value_d=0;
    float value_f=0;

public:
    virtual char *get_data(signal *sig)=0;
    virtual char *get_command(signal *sig)=0;
    virtual char get_value_c(signal *sig)=0;
    virtual int get_value(signal *sig)=0;
    virtual float get_value_f(signal *sig)=0;
    virtual double get_value_d(signal *sig)=0;
};

class mod_output{
protected :
    char *sql_string=0;
    char *data=0;
    char value_c=0;
    int value=0;
    double value_d=0;
    float value_f=0;

public:
    virtual bool *set_data(signal *sig)=0;
    virtual bool *set_command(signal *sig)=0;
    virtual bool set_value_c(signal *sig)=0;
    virtual bool set_value(signal *sig)=0;
    virtual bool set_value_f(signal *sig)=0;
    virtual bool set_value_d(signal *sig)=0;
};


class mod{
private:
    string name;
    long id;
    long root_id;
    ioctrl io = all;

protected :
    int command=NC;
    boost::thread *thread_in=nullptr,*thread_out=nullptr;
public:
    mod_common *com_p;
    bool setid(long l);
    long getid();

    string getname();
    bool setname(string str);

    ioctrl getmodio();
    void setmodio(ioctrl io);

    mod_input *input=nullptr;
    mod_output *ouput=nullptr;

    bool iteration(mod_common *com_p);
};

